package world.criver.imagecompresstool.application;

import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.image.WritableImage;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.robot.Robot;
import javafx.scene.text.Font;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import world.criver.imagecompresstool.data.daMaterial;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class appColorPicker extends Application {
    private double move_x=0,move_y=0,x=0,y=0;
    private int xx,yy,r,g,b;
    private Timer timer=null;
    private Screen screen=Screen.getPrimary();
    private GraphicsContext gc;
    private Canvas can;
    private Stage stage;
    private Image shot;
    private PixelReader pr;
    private Color color;
    private String rgb="",hex="";
    @Override
    public void start(Stage stage) throws Exception {
        Pane bp=new Pane();
        bp.setPrefWidth(screen.getBounds().getWidth());
        bp.setPrefHeight(screen.getBounds().getHeight());
        bp.setOnMouseMoved(e->{
            move_x=e.getX();
            move_y=e.getY();
        });
        bp.setOnMouseClicked(e->{
            if(e.getButton()==MouseButton.PRIMARY){//左键
                toClipBoard(rgb);
            }else if(e.getButton()==MouseButton.SECONDARY){//右键
                toClipBoard(hex);
            }
        });
        ImageView preview=new ImageView(getShot());
        shot=preview.getImage();
        pr=shot.getPixelReader();
        preview.setFitWidth(screen.getBounds().getWidth());
        preview.setFitHeight(screen.getBounds().getHeight());
        preview.setLayoutX(0);
        preview.setLayoutY(0);
        bp.getChildren().add(preview);
        can=new Canvas();
        can.setWidth(120);
        can.setHeight(160);
        can.setLayoutX(0);
        can.setLayoutY(0);
        gc=can.getGraphicsContext2D();
        gc.setFont(new Font("system",10));
        gc.setImageSmoothing(false);
        bp.getChildren().add(can);
        stage.setScene(new Scene(bp));
        stage.setWidth(screen.getBounds().getWidth());
        stage.setHeight(screen.getBounds().getHeight());
        stage.setFullScreen(true);
        stage.setFullScreenExitHint("");
        stage.fullScreenProperty().addListener((a,b,c)->{
            if(c)return;
            close();
        });
        stage.setTitle("取色");
        stage.getIcons().add(daMaterial.icon_32x32);
        stage.initStyle(StageStyle.TRANSPARENT);
        stage.show();
        this.stage=stage;
        timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if(gc==null || pr==null)return;
                x=move_x+8;
                y=move_y+8;
                if(x>screen.getBounds().getWidth()-128)x=move_x-128;
                if(y>screen.getBounds().getHeight()-168)y=move_y-168;
                can.setLayoutX(x);
                can.setLayoutY(y);
                gc.clearRect(0,0,120,160);
                //放大图
                xx= (int) move_x;
                yy= (int) move_y;
                gc.drawImage(shot,-xx*10+55,-yy*10+55,screen.getBounds().getWidth()*10,screen.getBounds().getHeight()*10);
                //清理下半部分
                gc.clearRect(0,120,120,40);
                //中心线
                gc.setLineWidth(10);
                gc.setStroke(new Color(0,1,0,0.1));
                gc.strokeLine(0,60,50,60);
                gc.strokeLine(70,60,120,60);
                gc.strokeLine(60,0,60,50);
                gc.strokeLine(60,70,60,120);
                //放大显示框
                gc.setStroke(Color.WHITE);
                gc.setLineWidth(1);
                gc.strokeRect(0,0,120,120);
                //信息区背景
                gc.setFill(new Color(0,0,0,0.5));
                gc.fillRect(0,121,120,40);
                //获取颜色
                color=pr.getColor(xx,yy);
                gc.setFill(Color.WHITE);
                r=(int)(color.getRed()*255);
                g=(int)(color.getGreen()*255);
                b=(int)(color.getBlue()*255);
                rgb=r+","+g+","+b;
                hex=formatHex(r)+formatHex(g)+formatHex(b);
                gc.fillText("rgb:"+rgb+" (左键复制)",2,136);
                gc.fillText("hex:#"+hex+" (右键复制)",2,156);
            }
        },new Date(),50);
    }
    private String formatHex(int n){
        String a=Integer.toHexString(n).toUpperCase();
        if(a.length()==1)return "0"+a;
        return a;
    }
    private Image getShot(){
        Robot ro=new Robot();
        WritableImage wi=new WritableImage((int) screen.getBounds().getWidth(), (int) screen.getBounds().getHeight());
        Rectangle2D rect=screen.getBounds();
        wi=ro.getScreenCapture(wi,rect);
        return wi;
    }
    private void toClipBoard(String content){
        Clipboard c=Clipboard.getSystemClipboard();
        ClipboardContent con=new ClipboardContent();
        con.putString(content);
        c.setContent(con);
        close();
    }
    private void close(){
        this.stage.setFullScreen(false);
        this.stage.hide();
        this.stage.close();
        if(timer!=null)timer.cancel();
    }
}
